package com.wjx.article_sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjx.article_sys.dao.ArticleMapper;
import com.wjx.article_sys.entity.Article;
import com.wjx.article_sys.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements IArticleService {


    @Autowired
    private HttpServletResponse response;
    @Autowired
    private HttpSession session;
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public PageInfo<Article> queryAllArticle(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Article> articlePageInfo = new PageInfo<>(articleMapper.queryAllArticle());
        return articlePageInfo;
    }

    @Override
    public Article queryArticleById(Integer id) {
        return articleMapper.queryArticleById(id);
    }

    @Override
    public void addArticle(String title, String content) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        if (articleMapper.queryArticleByTitle(title) != null)
            response.getWriter().write("<script>alert('该文章标题已存在！');" +
                    "window.location.href='/back/toEditArticle?title=" + title +
                    "&content=" + content + "'</script>");
        else {
            String author = session.getAttribute("userName").toString();
            SimpleDateFormat simpleDateFormat =
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String postTime = simpleDateFormat.format(new Date());
            try {
                articleMapper.addArticle(title, content, author, postTime);
                response.getWriter().write("<script>alert('文章发布成功');" +
                        "window.location.href='/?contentSize=6'</script>");
            } catch (Exception e) {
                e.printStackTrace();
                response.getWriter().write("<script>alert('文章发布失败');" +
                        "window.location.href='/?contentSize=6'</script>");
            }
        }
    }

    @Override
    public void updateArticleById(Integer id, String title, String content) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        if (articleMapper.queryArticleByTitle(title) != null &&
                !title.equals(articleMapper.queryArticleById(id).getTitle()))
            response.getWriter().write("<script>alert('该文章标题已存在！');" +
                    "window.location.href='/back/toEditArticle?title=" + title +
                    "&content=" + content + "&aId="+id+"&isModify=1'</script>");
        else {
            try {
                articleMapper.updateArticleById(id, title, content);
                response.getWriter().write("<script>alert('文章修改成功');" +
                        "window.location.href='/?contentSize=6'</script>");

            } catch (Exception e) {
                e.printStackTrace();
                response.getWriter().write("<script>alert('文章修改失败');" +
                        "window.location.href='/?contentSize=6'</script>");
            }
        }
    }

    @Override
    public void deleteArticleById(Integer id) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        try {
            articleMapper.deleteArticleById(id);
            response.getWriter().write("<script>alert('文章删除成功');" +
                    "window.location.href='/?contentSize=6'</script>");
        } catch (Exception e) {
            response.getWriter().write("<script>alert('文章删除失败');" +
                    "window.location.href='/?contentSize=6'</script>");
        }

    }

    @Override
    public List<Integer> articleCount(){
        List<Integer> countList = new ArrayList<Integer>();
        Integer  allArticleCount = articleMapper.articleAllCount();
        String author = session.getAttribute("userName").toString();
        Integer  userArticleCount = articleMapper.countArticleByAuthor(author);
        countList.add(0,allArticleCount);
        countList.add(1,userArticleCount);
        return countList;
    }
}
